# -*- coding: utf-8 -*-
'''
Created on May 21st, 2011

@author: manux
'''

from dao.DaoChemin import DaoChemin

class DaoElement():
    '''
    liste de compétences
    '''

    def __init__(self, type, race):
        '''
        Constructor
        ''' 
        self.pType = type
        self.pRace = race
        self._chemin = DaoChemin().cheminVersDao
        self._line = []
        
        self.objet = ""
        self.type = ""
        self.hauteur = 0
        self.constitution = 0
        self.taille = 0
        self.degats = 0.
        self.franchissable = True
        self.joueur = 0
        self.endurance = 0
        self.intelligence = 0
        self.energie = 0
        self.cible = ""
        self.competence = {"Mouvement": 1, "Passe son tour": 1}
        self.nom = ""
        
        #################################################################
        '''Construction des personnages'''
        if (self.pType == "Guerrier" and self.pRace == "Nain"):
            self.fillCompetences("CompRankor")
        
        if (self.pType == "Archer" and self.pRace == "Elfe"):
            self.fillCompetences("CompShu")
        
        if (self.pType == "Mage" and self.pRace == "Mort-Vivant"):
            self.fillCompetences("CompAsura")
        
        if (self.pType == "DeathKnight" and self.pRace == "BloodElfe"):
            self.fillCompetences("CompMantas")
        
        #################################################################
        '''Construction du terrain de printemps'''
        if (self.pType == "Terrain" and self.pRace == "Eau"):
            self.fillField('DataTypeTerrain', 2)
        
        if (self.pType == "Terrain" and self.pRace == "Herbe"):
            self.fillField('DataTypeTerrain', 4)
        
        if (self.pType == "Terrain" and self.pRace == "Rocher"):
            self.fillField('DataTypeTerrain', 6)
        
        if (self.pType == "Terrain" and self.pRace == "Sable"):
            self.fillField('DataTypeTerrain', 7)
        
        #################################################################
        '''Construction du terrain d'hiver'''
        if (self.pType == "Terrain" and self.pRace == "Arbre"):
            self.fillField('DataTypeTerrain', 1)
        
        if (self.pType == "Terrain" and self.pRace == "Glace"):
            self.fillField('DataTypeTerrain', 3)
        
        if (self.pType == "Terrain" and self.pRace == "Neige"):
            self.fillField('DataTypeTerrain', 5)
    
    
    def fillCompetences(self, nomFichier):
        chemin = self._chemin
        chemin = str(chemin) + nomFichier
        try:
            f = open(chemin, 'r')
        except IOError:
            print "Impossible d'ouvrir %s" % file
        listeComp = f.readlines()
        
        self.objet = listeComp[0][:-1]
        self.type = listeComp[1][:-1]
        self.hauteur = int(listeComp[2][:-1])
        self.constitution = int(listeComp[3][:-1])
        self.taille = float(listeComp[4][:-1])
        self.degats = int(listeComp[5][:-1])
        if(listeComp[6][:-1] == 'False'):
            self.franchissable = False
        self.joueur = int(listeComp[7][:-1])
        self.endurance = int(listeComp[8][:-1])
        self.intelligence = int(listeComp[9][:-1])
        self.energie = int(listeComp[10][:-1])
        self.cible = listeComp[11][:-1]
        '''
        Exemple : listeComp[12] => "Frappe:1,Brise Genoux:1"
        doit remplir self.competence comme suit : {'Frappe': 1, 'Brise Genoux': 1}
        '''
        V = listeComp[12][:-1]
        temp = V.split(',')
        for x in temp:
            y = x.split(':')
            self.competence[y[0]] = int(y[1])
        
        self.nom = listeComp[13][:-1]
        f.close()
    
    
    def fillField(self, file, x):
        chemin = self._chemin
        chemin = str(chemin) + file
        try:
            f = open(chemin, 'r')
        except IOError:
            print "Impossible d'ouvrir %s" % file
        self._line = f.readlines()
            
        self.objet = self._line[0][:-1]
        self.type = self._line[x][:-1]
        self._goThrough(x)
        f.close()
    
    
    def _goThrough(self, x):
        if(x in (1, 2, 3, 6)):
            self.franchissable = False
        else:
            self.franchissable = True
